package etr.bk.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

import etr.bk.db.WrapperLPan;

public class UnloadDialog extends JDialog implements ActionListener {

	private MainListener mListener;

	private JButton buttonOK;
	private JButton buttonCancel;
	private JButton buttonCancelExport;

	private JRadioButton allImages;
	private JRadioButton itemsImages;
	private ButtonGroup groupCh;
	private JComboBox<String> listOfItem;
	private JTextField textField;
	private JButton buttonBrowse;
	private JProgressBar progressBar;

	ArrayList<WrapperLPan> listItemObj = null;

	private JFrame parent;
	private JFileChooser dirChooser;
	private File targetDir = null;

	public UnloadDialog(JFrame parent) {
		super(parent, "Unload Dialog", false);

		this.parent = parent;

		mListener = MainListener.getListener();

		dirChooser = new JFileChooser();
		dirChooser.setCurrentDirectory(new File("."));

		// -------------------------------------------------------
		Point parentPos = parent.getLocationOnScreen();
		this.setLayout(new FlowLayout());
		this.setBounds((int) parentPos.getX() + 250,
				(int) parentPos.getY() + 200, 350, 215);

		// -----PANELLLLLL------------------------
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(340, 140));
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		panel.setBorder(BorderFactory.createTitledBorder(border,
				"Unload Images "));

		// -----------OK CANCEL-----------------------
		buttonOK = new JButton("OK");
		buttonCancel = new JButton("Cancel");
		buttonCancelExport = new JButton("Interrupt Export");
		buttonCancelExport.setVisible(false);

		mListener.setButtonCancelExport(buttonCancelExport);
		mListener.setButtonUnload(buttonOK);
		buttonOK.addActionListener(mListener);
		buttonCancelExport.addActionListener(mListener);

		// --------RADIO BUTTON------------------
		allImages = new JRadioButton("All");
		allImages.setSelected(true);
		itemsImages = new JRadioButton("For item ");

		groupCh = new ButtonGroup();
		groupCh.add(allImages);
		groupCh.add(itemsImages);

		// -----JCOMBOBOX-------------------

		listOfItem = new JComboBox<String>();
		listOfItem.setPreferredSize(new Dimension(200, 30));
		listOfItem.setEnabled(false);

		// -----Text field---------------------
		textField = new JTextField("Path for images", 22);

		// --------Browse button----------------
		buttonBrowse = new JButton("   ...   ");
		buttonBrowse.addActionListener(this);

		// -----Progress bar---------------------
		progressBar = new JProgressBar();
		progressBar.setPreferredSize(new Dimension(245, 20));
		progressBar.setMinimum(0);

		// -------labels--------------------------------
		JLabel labelTarget = new JLabel(
				"Choose target folder:                                                                  ");
		JLabel labelProgress = new JLabel("Progress :  ");

		// -------listeners-----------------------------

		buttonCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				UnloadDialog.this.setVisible(false);
			}
		});

		allImages.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				UnloadDialog.this.listOfItem.setEnabled(false);
			}
		});

		itemsImages.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				UnloadDialog.this.filListItem();
				UnloadDialog.this.listOfItem.setEnabled(true);
			}
		});
		// -------------------------------------------------------
		panel.add(allImages);
		panel.add(itemsImages);
		panel.add(listOfItem);
		panel.add(labelTarget);
		panel.add(textField);
		panel.add(buttonBrowse);
		panel.add(labelProgress);
		panel.add(progressBar);

		this.add(panel);
		this.add(buttonCancelExport);
		this.add(buttonOK);
		this.add(buttonCancel);
		
		this.filListItem();
		
	}

	public void actionPerformed(ActionEvent arg0) {

		this.dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		this.dirChooser.showDialog(this.parent, "This DIR");
		targetDir = dirChooser.getSelectedFile();
		if (targetDir == null)
			System.out.println("NULL in unloader");
		if (targetDir != null)
			this.textField.setText(targetDir.getPath());

	}

	public File getTargetPath() {

		if (targetDir != null
				&& textField.getText().equals(targetDir.getPath())) {
			return targetDir;
		} else {
			File filTmp = new File(this.textField.getText());
			if (filTmp.exists() && filTmp.isDirectory()) {
				System.out.println(filTmp.getPath()
						+ "YYYYESSSS!!!!!!!!!!!!!!!!!!!!!!!1"
						+ filTmp.toString());
				return filTmp;
			}
		}
		return null;
	}

	public Integer getItemID() {
		// if return null unload all images
		if (this.allImages.isSelected())
			return null;
		else {
			int index = this.listOfItem.getSelectedIndex();
			System.out.println("%%% " + index);
			WrapperLPan tmpObj = this.listItemObj.get(index);
			Integer rez = tmpObj.getID();
			return rez;
		}
	}

	public void setAllInactive() {
		this.buttonOK.setVisible(false);
		this.buttonCancel.setVisible(false);
		this.buttonCancelExport.setVisible(true);

		this.buttonBrowse.setEnabled(false);
		this.textField.setEnabled(false);
		this.itemsImages.setEnabled(false);
		this.allImages.setEnabled(false);
		this.listOfItem.setEnabled(false);

		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}

	public void setAllActive() {
		this.buttonCancelExport.setVisible(false);
		this.buttonOK.setVisible(true);
		this.buttonCancel.setVisible(true);
		this.buttonBrowse.setEnabled(true);
		this.textField.setEnabled(true);
		this.itemsImages.setEnabled(true);
		this.allImages.setEnabled(true);
		this.listOfItem.setEnabled(true);

		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
	}

	public void filListItem() {

		listItemObj = mListener.getInfoForUnloader();
		System.out.println("UNLOADER "+listItemObj.size());
		String[] listForComboBox = new String[listItemObj.size()];
		int i = 0;
		for (WrapperLPan tmp : listItemObj) {
			listForComboBox[i++] = tmp.getStr();
		}

		if (listForComboBox.length != 0) {
			itemsImages.setEnabled(true);
			listOfItem.removeAllItems();
			for (String tmp : listForComboBox)
				listOfItem.addItem(tmp);

			if (this.allImages.isSelected()){
				System.out.println("==== "+this.allImages.isSelected());
				listOfItem.setEnabled(false);
			}
			else listOfItem.setEditable(false);
			
			buttonBrowse.setEnabled(true);
			textField.setEnabled(true);
			buttonOK.setEnabled(true);
			textField.setText("Path for images");
		} else {
			allImages.setSelected(true);
			listOfItem.removeAll();
			listOfItem.setEnabled(false);
			itemsImages.setEnabled(false);
			buttonBrowse.setEnabled(false);
			textField.setEnabled(false);
			buttonOK.setEnabled(false);
			textField.setText("NO images for unload!!!");

		}

	}

	public JProgressBar getProgressBar() {
		return this.progressBar;
	}

}